local jizhu = fk.CreateSkill{
  name = "rfenghou_3d13j__jizhu",
}

local spec = { ---@type TrigSkelSpec<fun(self: TriggerSkill, event: UseCardEvent, target: ServerPlayer, player: ServerPlayer, data: UseCardData):any>
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(jizhu.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = room:getNCards(1)
    room:turnOverCardsFromDrawPile(player, ids, jizhu.name)
    local cardlist = data.card:isVirtual() and data.card.subcards or {data.card.id}
    if Fk:translate(Fk:getCardById(ids[1]).trueName, "zh_CN"):len() == Fk:translate(data.card.trueName, "zh_CN"):len()
    and #cardlist > 0 and table.every(cardlist, function (id) return room:getCardArea(id) == Card.Processing end) then
      local to = room:askToChoosePlayers(player, {
        targets = table.filter(room.alive_players, function(p)
          return p ~= room.current end),
        min_num = 1,
        max_num = 1,
        prompt = "#rfenghou_3d13j__jizhu-choose",
        skill_name = jizhu.name,
        cancelable = false
      })
      if #to > 0 then
        local cards = room:getSubcardsByRule(data.card, { Card.Processing })
        table.insertIfNeed(cards, ids[1])
        room:moveCardTo(cards, Card.PlayerHand, to[1], fk.ReasonGive, jizhu.name, nil, true, player)
      end
    end
    room:cleanProcessingArea(ids, jizhu.name)
  end,
}

jizhu:addEffect(fk.CardUsing, spec)
jizhu:addEffect(fk.CardResponding, spec)

Fk:loadTranslationTable{
  ["rfenghou_3d13j__jizhu"] = "籍注",
  [":rfenghou_3d13j__jizhu"] = "当你使用或打出牌时，你可以亮出牌堆顶的牌，若二者牌名字数相等，你将这两张牌交给一名非当前回合角色。",

  ["#rfenghou_3d13j__jizhu-choose"] = "籍注：请将亮出牌交给一名非当前回合角色！",
}

jizhu:addTest(function(room, me)
  local comp2 = room.players[2]
  local ex_nihilo = room:printCard("ex_nihilo")
  local slash = Fk:getCardById(1)
  local snatch = room:printCard("snatch")
  FkTest.RunAddSkills(me, jizhu.name)

  --- test1: me开启一个回合，使用无中生有，亮出牌堆顶的杀
  --- 期望： （无事发生）

  FkTest.setNextReplies(me, {
    FkTest.replyCard(ex_nihilo, { me }),
    "1"
  })

  FkTest.runInRoom(function()
    room:moveCardTo(slash, Card.DrawPile)
    me:gainAnExtraTurn(false, nil, { Player.Play })
  end)

  for _, other in ipairs(room:getOtherPlayers(me, false)) do
    lu.assertEquals(other:getHandcardNum(), 0)
  end

  --- test2: me开启一个回合，使用无中生有，亮出牌堆顶的顺手牵羊
  --- 期望： 只能将无中生有和顺手牵羊交给非me角色

  FkTest.setNextReplies(me, {
    FkTest.replyCard(ex_nihilo, { me }),
    "1"
  })

  FkTest.setRoomBreakpoint(me, "AskForUseActiveSkill", function(data)
    return data[2] == "#rfenghou_3d13j__jizhu-choose"
  end)

  FkTest.runInRoom(function()
    room:moveCardTo(snatch, Card.DrawPile)
    me:gainAnExtraTurn(false, nil, { Player.Play })
  end)
  local handler = ClientInstance.current_request_handler --[[@as ReqActiveSkill]]

  lu.assertIsFalse(handler:targetValidity(me.id))

  FkTest.setNextReplies(me, {
    FkTest.replyChoosePlayer({ comp2 })
  })

  FkTest.resumeRoom()

  lu.assertEquals(comp2:getCardIds("h"), {ex_nihilo.id, snatch.id})
end)

return jizhu